﻿using System;
using System.Collections.Generic;
using System.Text;
using MiragePWF.NH.Audit;
using MiragePWF.NH.Common;
using Wolfut.MirageEE.Kernal.Common;

namespace Wolfut.MirageEE.Kernal.Audit
{
    public class MirageEEAuditProvider :DefaultAuditProvider

    {
        public override void OnPreInsert(IAuditable entity)
        {
            base.OnPreInsert(entity);
            (entity as IAuditableEx).LastUpdatedBy = "peter";
            (entity as IAuditableEx).CreatedBy = "peter";
        }
        public override void OnPreUpdate(IAuditable entity)
        {
            base.OnPreUpdate(entity);
            (entity as IAuditableEx).LastUpdatedBy = "peter";
        }
        public override bool OnPreInsert(IAuditable entity, string[] propertyNames, object[] state)
        {
            bool retFlag = false;


            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (ConstantEx.LastUpdatedBy.Equals(propertyNames[i]))
                {
                    state[i] = "peter";
                    retFlag = true;
                    continue;
                }
                if (ConstantEx.CreatedBy.Equals(propertyNames[i]))
                {
                    state[i] = "peter";
                    retFlag = true;
                    continue;
                }
            }
            return retFlag && base.OnPreInsert(entity, propertyNames, state);
        }
        public override bool OnPreUpdate(IAuditable entity, string[] propertyNames, object[] currentState)
        {
            bool retFlag = false;

            for (int i = 0; i < propertyNames.Length; i++)
            {
                if (ConstantEx.LastUpdatedBy.Equals(propertyNames[i]))
                {
                    currentState[i] = "peter";
                    retFlag = true;
                    continue;
                }
         }

            return retFlag &&base.OnPreUpdate(entity, propertyNames, currentState);
        }
    }
}
